package com.my.chapter11;

import java.util.HashSet;
import java.util.Set;
import java.util.TreeSet;

public class practice22 {
    private static Set<Word> set = new HashSet<Word>();

    static void countWords(String[] str){
        boolean flag = false;
        for(String s:str){
            flag = false;
            for(Word w:set){
                if(s.equals(w.str)){
                    w.setCount();
                    flag = true;
                    break;
                }
            }
            if(!flag){
                Word word = new Word();
                word.str = s;
                word.setCount();
                set.add(word);
            }
        }
    }

    public static void main(String[] args) {
        String s = "A contented mind is the greatest blessing a man can enjoy in this world";
        String[] str = s.split(" ");
        countWords(str);
        System.out.println(set);
    }
}

class Word{
    String str;
    int count = 0;

    void setCount(){
        count++;
    }

    @Override
    public String toString() {
        return str + "：" + count + ";";
    }
}
